//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientSetupHostage.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_instance_id","_parameters","_freed_msg","_die_msg","_shared_free","_shared_alive","_free_code","_die_code","_evt_idx","_action_id","_act_id"
		,"_obj_var_freed","_is_freed","_action_display_timer","_move_loop"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);
	if (_object isKindOf "Man") then {
		_freed_msg = [_parameters, "FREED_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_shared_free = [_parameters, "SHARED_FREE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_free_code = [_parameters, "FREE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_free = _shared_free call FFUNC(getStringVar);
		_shared_alive = _shared_alive call FFUNC(getStringVar);
	
		_act_id = [_object,_action,"HOSTAGE_ACTION",-1] call FFUNC(actionGetLocal);
		if (_act_id >= 0) then {
			TRACE_1("Remove Action",_act_id);
			_object removeAction _act_id;
		};
		
		_obj_var_freed = format["mission_person_freed_%1",_action_id];
		_is_freed = OGETVAR(_object,_obj_var_freed,false);
		If (isNil "_is_freed") then {
			_is_freed = false;
		};
		if (!_is_freed) then {
			_object setCaptive true;
			_action_title = ("STR_TMF_RESCUE_ACTION") call FFUNC(translate);
			_arguments = [];
			_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
			_action_display_timer = QGVAR(action_display_timer);
			_object setVariable [_action_display_timer,0];
			_condition = format["(alive _target) && {captive _target} && {vehicle player == player} && {(_target getVariable '%1' < time)}",_action_display_timer];
			_act_id = _object addAction [_action_title, QPATH(addAction.sqf),_arguments,10,True,True,"",_condition];
			// We set the variable as local because it is dependent on the computer running it.
			[_object,_action,"HOSTAGE_ACTION",_act_id] call FFUNC(actionSetLocal);
			TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
			if (alive _object) then {
				_move_loop = [_object,_action,"HOSTAGE_MOVE_LOOP",false] call FFUNC(actionGetLocal);
				// We only need to add the loop once
				if (!_move_loop) then {
					[_object,_action,"HOSTAGE_MOVE_LOOP",true] call FFUNC(actionSetLocal);
					// The following code will play the captive animation until the hostage is freed or dead.
					[_object, _action_id] spawn {
						private ["_hostage","_curr_anim","_action_id","_obj_var","_timeout"];
						_hostage = _this select 0;
						_action_id = _this select 1;
						_obj_var = format["mission_person_freed_%1",_action_id];
						TRACE_2("Start Waiting",_hostage, (_hostage isKindOf "Woman"));
						While {captive _hostage && alive _hostage && !(OGETVAR(_hostage,_obj_var,false))} do {
							_move = format["AmovPercMstpSsurWnonDnon",(floor(random(3))+1)];
							// Women need another animation.
							if (_hostage isKindOf "Woman") then {_move = format["AmovPercMstpSsurWnonDnon",(floor(random(3))+1)];};
							if (_move != animationState _hostage) then {
								_hostage switchMove _move;
							};
							//_animstate = animationState _hostage;
							// This line was useful when we had no fixed move
							//waituntil {_move != animationState _hostage};
							sleep 3.234;
						};
						_hostage setCaptive false;
						TRACE_2("End Waiting",_hostage, alive _hostage);
						if (alive _hostage) then {
							_move = "AmovPercMstpSsurWnonDnon_AmovPercMstpSnonWnonDnon";
							_hostage switchMove "AmovPercMstpSsurWnonDnon_AmovPercMstpSnonWnonDnon";
							_timeout = time + 10;
							waituntil {_move != animationState _hostage || time > _timeout};
							_hostage switchMove "AidlPercMstpSnonWnonDnon_AI";
							TRACE_1("Switched move",animationState _hostage);
						};
					};
				};
			};
		};
	};
} else {
	ERROR("Object not set");
};
TRACE("End");
TMFPROFILERSTOP;